package com.mf.ergate.service.auth.impl;

import com.mf.ergate.api.auth.model.dto.RoleResourceRelationQueryDto;
import com.mf.ergate.dao.auth.ResourceInfoDao;
import com.mf.ergate.domain.model.auth.ResourceInfoEntity;
import com.mf.ergate.domain.model.auth.RoleResourceRelationEntity;
import com.mf.ergate.service.GenericService;
import com.mf.ergate.service.auth.ResourceInfoService;
import com.mf.ergate.service.auth.RoleResourceRelationService;
import org.apache.commons.collections4.CollectionUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * 资源信息Service层实现类
 * User: zhaoming
 * DateTime: 2018-10-10
 * To change this template use File | Settings | File Templates.
 **/
@Service
public class ResourceInfoServiceImpl extends GenericService<ResourceInfoEntity> implements ResourceInfoService {

    private Logger logger = LoggerFactory.getLogger(getClass());

    @Autowired
    private ResourceInfoDao resourceInfoDao;
    @Autowired
    private RoleResourceRelationService roleResourceRelationService;

    /**
     * 根据应用类型编码与用户编号查询
     * @param appTypeCode 应用类型编号
     * @param userNo 用户编号
     * @return 资源信息列表
     */
    public List<ResourceInfoEntity> selectByAppTypeCodeAndUserNo(String appTypeCode, String userNo){
        return resourceInfoDao.selectByAppTypeCodeAndUserNo(appTypeCode, userNo);
    }

    /**
     * 删除资源及相关信息(逻辑删除)
     * @param resourceInfoEntity 资源信息Entity
     * @return 执行结果
     */
    @Transactional
    public Boolean deleteCompleteByUniqueNo(ResourceInfoEntity resourceInfoEntity){
        //删除角色与资源关联信息
        RoleResourceRelationQueryDto roleResourceRelationQueryDto = new RoleResourceRelationQueryDto();
        roleResourceRelationQueryDto.setResourceNo(resourceInfoEntity.getUniqueNo());
        List<RoleResourceRelationEntity> roleResourceRelationEntities = roleResourceRelationService.selectByCondition(roleResourceRelationQueryDto);
        if (CollectionUtils.isNotEmpty(roleResourceRelationEntities)) {
            for (RoleResourceRelationEntity data : roleResourceRelationEntities) {
                data.setUpdateTime(resourceInfoEntity.getUpdateTime());
                data.setUpdateUserNo(resourceInfoEntity.getUpdateUserNo());
                data.setUpdateUserName(resourceInfoEntity.getUpdateUserName());
            }
            roleResourceRelationService.deleteByUniqueNos(roleResourceRelationEntities);
        }
        return resourceInfoDao.deleteByUniqueNo(resourceInfoEntity);
    }
}
